package com.cloud.business.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.business.controller.BaseController;
import com.cloud.business.entity.sys.SysMenu;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysmenuvm.SysMenuNavTreeVM;
import com.cloud.business.model.sys.sysmenuvm.SysMenuQueryModel;
import com.cloud.business.model.sys.sysmenuvm.SysMenuTreeVM;
import com.cloud.business.service.sys.ISysMenuService;
import com.cloud.core.common.HttpContentResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统菜单 前端控制器
 * </p>
 *
 * @author dh
 * @since 2020-03-21
 */
@RestController
@Api(tags = "菜单模块")
@RequestMapping("/api/sysMenu")
public class SysMenuController extends BaseController {
    @Autowired
    private ISysMenuService ISysMenuService;

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param queryModel
     * @return
     */
    @ApiOperation(value = "获取分页列表")
    @PostMapping("/getListByPage")
    public HttpContentResult getListByPage(@RequestBody SysMenuQueryModel queryModel) {
        ResultMsg msg = new ResultMsg();
        try {
            queryModel.setOperationId(getOperationId());
            IPage<SysMenu> page = new Page<>(queryModel.getPageNum(), queryModel.getRecordNum());
            ISysMenuService.getListByPage(queryModel, page, msg);
            return HttpContentResult.successResult(msg.getMsg(), page.getTotal(), page.getRecords());
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param model
     * @return
     * @author Rex
     */
    @ApiOperation(value = "创建")
    @PostMapping("/create")
    public HttpContentResult create(@RequestBody SysMenu model) {
        ResultMsg msg = new ResultMsg();
        try {
            model.setCreateUserId(getOperationId());
            Integer objectId = ISysMenuService.create(model, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, objectId);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param model
     * @return
     * @author Rex
     */
    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public HttpContentResult update(@RequestBody SysMenu model) {
        ResultMsg msg = new ResultMsg();
        try {
            model.setModificationUserId(getOperationId());
            Integer objectId = ISysMenuService.update(model, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, objectId);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param objectId
     * @return
     */
    @ApiOperation(value = "获取单条")
    @GetMapping("/getById")
    public HttpContentResult getById(Integer objectId) {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            SysMenu SysMenu = ISysMenuService.getById(operationId, objectId, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, SysMenu);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @return
     * @author Rex
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/deleteByList")
    public HttpContentResult deleteByList(@RequestBody DeletePost deletePost) {
        ResultMsg msg = new ResultMsg();
        try {
            deletePost.setOperationId(getOperationId());
            Integer count = ISysMenuService.deleteByList(deletePost, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, count);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 获取菜单树--返回权限是否分配

    /**
     * 获取菜单树--返回权限是否分配
     *
     * @return
     */
    @ApiOperation(value = "获取菜单树--返回权限是否分配")
    @GetMapping("/getMenuTree")
    public HttpContentResult getMenuTree() {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            List<SysMenuTreeVM> sysMenuTreeVMList = ISysMenuService.getMenuTree(operationId);
            return HttpContentResult.successResult(msg.getMsg(), 1L, sysMenuTreeVMList);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 获取菜单树--返回权限是否分配

    /**
     * 获取用户菜单树--返回对应权限码
     *
     * @return
     */
    @ApiOperation(value = "获取用户菜单树--返回对应权限码")
    @GetMapping("/getUserMenuTree")
    public HttpContentResult getUserMenuTree() {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            List<SysMenuNavTreeVM> sysMenuNavTreeVMList = ISysMenuService.getUserMenuTree(operationId);
            return HttpContentResult.successResult(msg.getMsg(), 1L, sysMenuNavTreeVMList);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion
}
